package zgrjb.snbq.teach_platform.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zgrjb.snbq.teach_platform.dto.model.AttendDO;
import zgrjb.snbq.teach_platform.dto.model.ClassDO;
import zgrjb.snbq.teach_platform.dto.model.User;
import zgrjb.snbq.teach_platform.interceptor.LoginInterceptor;
import zgrjb.snbq.teach_platform.mapper.AttendMapper;
import zgrjb.snbq.teach_platform.mapper.ClassMapper;
import zgrjb.snbq.teach_platform.service.AttendService;
import zgrjb.snbq.teach_platform.util.IDUtil;
import zgrjb.snbq.teach_platform.util.JsonData;

import java.util.List;

@Service
public class AttendServiceImpl implements AttendService {

    @Autowired
    public AttendMapper attendMapper;

    @Autowired
    private ClassMapper classMapper;

    @Override
    public JsonData add(Long classId) {
        User user = LoginInterceptor.getCurrentUser();
        ClassDO classDO = classMapper.selectById(classId);
        AttendDO attendance = new AttendDO();
        attendance.setClassId(classId);
        attendance.setStudentId(user.getId());
        attendance.setSubject(classDO.getSubject());
        attendance.setClassName(classDO.getName());
        attendance.setStudentName(user.getName());
        attendance.setStudentPhone(user.getPhone());
        attendMapper.insert(attendance);
        return JsonData.buildSuccess();
    }

    @Override
    public JsonData allow(Long stuId, Long classId) {
        attendMapper.update(new UpdateWrapper<AttendDO>()
                .eq("student_id",stuId)
                .eq("class_id",classId)
                .set("allow",1));
        return JsonData.buildSuccess();

    }

    @Override
    public JsonData getAttendees(Integer allow, Long id) {
        List<AttendDO> list = attendMapper.selectList(new QueryWrapper<AttendDO>()
                .eq("class_id",id)
                .eq("allow",allow));
        return JsonData.buildSuccess(list);

    }
}
